<template>
    <div>
       <el-form :model="afficheForm" :rules="rules" ref="afficheForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="标题" prop="afficheName">
    <el-input v-model="afficheForm.afficheName" size="mini"></el-input>
  </el-form-item>
  <el-form-item label="公告介绍" prop="afficheDes">
    <el-input type="textarea" v-model="afficheForm.afficheDes"></el-input>
  </el-form-item>
  <el-form-item label="活动时间" prop="afficheTime">
    <el-date-picker
      v-model="afficheForm.afficheTime"
      type="datetime"
      value-format="yyyy-MM-dd hh:mm:ss"
      placeholder="选择日期时间">
    </el-date-picker>
  </el-form-item>
  <el-form-item label="活动状态" prop="afficheState">
    <el-switch v-model="afficheForm.afficheSate"></el-switch>
  </el-form-item>
  
  <el-form-item>
    <el-button type="primary" @click="submitForm('afficheForm')">立即创建</el-button>
    <el-button @click="resetForm('afficheForm')">重置</el-button>
  </el-form-item>
</el-form>
    </div>
  </template>
  <script>
  import {addAffiche} from '@/api'
  // 导入组件
 export default {
    name: 'addGoods',
    data() {
      return {
        afficheForm: {
          afficheName: '',
          afficheDes: '',
          afficheTime: '',
          afficheSate: false,
          },
        rules: {
            afficheName: [
            { required: true, message: '请输入公告标题', trigger: 'blur' },
            { min: 3,  message: '长度在 3 个字符以上', trigger: 'blur' }
          ],
          afficheDes: [
            { required: true, message: '请输入公告内容', trigger: 'change' }
          ]}
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data ={
              title:this.afficheForm.afficheName,
              introduce:this.afficheForm.afficheDes,
              addtime:this.afficheForm.afficheTime,
              status:this.afficheForm.afficheSate?1:0
            }
            addAffiche(data).then(res=>{
           
              if(res.code == 200){
               this.$message.success('添加成功')
            }else{
              this.$message.error('添加失败，请重试！')
            }
            })
          } else {
            this.$message.error("请正确输入表单信息!")
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
  </script>
  <style >

  </style>
  